package com.leoman.utils;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import it.sauronsoftware.jave.Encoder;
import it.sauronsoftware.jave.MultimediaInfo;
import org.apache.commons.fileupload.disk.DiskFileItem;
import org.apache.commons.lang.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.File;
import java.util.Map;

/**
 * 七牛工具类
 * Created by 涂奕恒 on 2016/11/22.
 */
public class QiNiuUtils {
    //设置好账号的ACCESS_KEY和SECRET_KEY
    public static String ACCESS_KEY = "paUdianW9M1WhtQ1VKIrT5bXVZFiTJ_I-k1xIWE9";

    public static String SECRET_KEY = "YK8icncMEa1_oCkW-5mji3exj-yywfrls2t78H0I";

    //要上传的空间
    public static String bucketName = "52hz";

    public static String url = "http://okzhysgh8.bkt.clouddn.com/";

    //密钥配置
    public static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

    //创建上传对象
    public static UploadManager uploadManager = new UploadManager();

    //实例化一个BucketManager对象
    public static BucketManager bucketManager = new BucketManager(auth);

    //简单上传，使用默认策略，只需要设置上传的空间名就可以了
    public static String getUpToken() {
        return auth.uploadToken(bucketName);
    }

    /**
     * 获取文件后缀
     */
    public static String getNamePlus(MultipartFile myFile) {
        CommonsMultipartFile cf = (CommonsMultipartFile) myFile; //这个myfile是MultipartFile的
        DiskFileItem fi = (DiskFileItem) cf.getFileItem();
        return fi.getContentType();
    }

    /**
     * 上传文件
     *
     * @param myFile
     */
    public static String upload(MultipartFile myFile) {
        try {
            CommonsMultipartFile cf = (CommonsMultipartFile) myFile; //这个myfile是MultipartFile的
            DiskFileItem fi = (DiskFileItem) cf.getFileItem();
            File file = fi.getStoreLocation();

            //调用put方法上传
            Response res = uploadManager.put(file, null, getUpToken());
            Map<String, Object> map = JsonUtil.jsontoMap(res.bodyString());
            return url + map.get("key");
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                e1.printStackTrace();
            }
            return "";
        }
    }

    /**
     * 上传文件
     *
     * @param file
     */
    public static String upload(File file) {
        try {
            //调用put方法上传
            Response res = uploadManager.put(file, null, getUpToken());
            Map<String, Object> map = JsonUtil.jsontoMap(res.bodyString());
            return url + map.get("key");
        } catch (QiniuException e) {
            Response r = e.response;
            // 请求失败时打印的异常的信息
            System.out.println(r.toString());
            try {
                //响应的文本信息
                System.out.println(r.bodyString());
            } catch (QiniuException e1) {
                e1.printStackTrace();
            }
            return "";
        }
    }

    /**
     * 根据key删除图片
     */
    public static void delete(String key) {
        try {
            //调用delete方法移动文件
            if (StringUtils.isNotBlank(key)) {
                bucketManager.delete(bucketName, key.replace(url, ""));
            }
        } catch (QiniuException e) {
            //捕获异常信息
            Response r = e.response;
            System.out.println(r.toString());
        }
    }

    public static Map<String, Object> getInfo(String url) {
        String result = HttpRequestUtil.sendGet(url, "imageInfo");
        return JsonUtil.jsontoMap(result);
    }

    public static Integer getVideoTime(String path) {
        File source = new File(path);
        Encoder encoder = new Encoder();
        try {
            MultimediaInfo m = encoder.getInfo(source);
            long ls = m.getDuration();
            long min = ls / 60000;
            System.out.println("此视频时长为:" + min + "分" + ((ls / 1000) - min * 60) + "秒！");
            return Integer.parseInt((ls / 1000) + "");
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static Integer getVideoTime(MultipartFile multipartFile) {
        CommonsMultipartFile cf = (CommonsMultipartFile) multipartFile;
        DiskFileItem fi = (DiskFileItem) cf.getFileItem();
        File source = fi.getStoreLocation();
        Encoder encoder = new Encoder();
        try {
            MultimediaInfo m = encoder.getInfo(source);
            long ls = m.getDuration();
            // System.out.println("此视频时长为:" + ls / 60000 + "分" + ((ls / 1000) - 60) + "秒！");
            return Integer.parseInt((ls / 1000) + "");
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    public static void main(String[] args) {
        System.out.println("语音长度：" + getVideoTime("F:/LiSA - oath sign_C32kbps.mp3") + "秒");
    }
}
